package com.bytedance.common.plugin.component;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.UserHandle;

import com.bytedance.common.plugin.a;
import com.bytedance.common.plugin.framework.PluginManager;
import com.bytedance.common.utility.reflect.Reflect;

@TargetApi(value = 14)
public class BaseProxyService extends Service {
    private PluginContextImpl mPluginContextImpl;
    private Reflect mPluginRef;
    private Service mPluginService;
    private Reflect mProxyRef;

    public BaseProxyService() {

    }

    public boolean bindService(Intent arg2, ServiceConnection arg3, int arg4) {
        boolean v0 = this.mPluginContextImpl != null ? this.mPluginContextImpl.bindService(arg2, arg3, arg4) : super.bindService(arg2, arg3, arg4);
        return v0;
    }

    public AssetManager getAssets() {
        AssetManager v0 = this.mPluginContextImpl != null ? this.mPluginContextImpl.getAssets() : super.getAssets();
        return v0;
    }

    protected String getPluginClass() {
        return a.a.get(this.getClass().getName());
    }

    public Resources getResources() {
        return this.mPluginContextImpl != null ? this.mPluginContextImpl.getResources() : super.getResources();
    }

    public Resources.Theme getTheme() {
        return super.getTheme();
    }

    public IBinder onBind(Intent arg2) {
        IBinder v0 = this.mPluginService != null ? this.mPluginService.onBind(arg2) : null;
        return v0;
    }

    public void onConfigurationChanged(Configuration arg2) {
        super.onConfigurationChanged(arg2);
        if (this.mPluginService != null) {
            this.mPluginService.onConfigurationChanged(arg2);
        }
    }

    public void onCreate() {
        super.onCreate();
        this.tryLoadPlugin();
        try {
            this.mPluginContextImpl = new PluginContextImpl(this.getBaseContext(), false);
        } catch (Throwable v0) {
        }

        if (this.mPluginService != null) {
            this.mPluginService.onCreate();
        }
    }

    public void onDestroy() {
        super.onDestroy();
        if (this.mPluginService != null) {
            this.mPluginService.onDestroy();
        }
    }

    public void onLowMemory() {
        super.onLowMemory();
        if (this.mPluginService != null) {
            this.mPluginService.onLowMemory();
        }
    }

    public void onRebind(Intent arg2) {
        super.onRebind(arg2);
        if (this.mPluginService != null) {
            this.mPluginService.onRebind(arg2);
        }
    }

    @Deprecated
    public void onStart(Intent arg2, int arg3) {
        super.onStart(arg2, arg3);
        if (this.mPluginService != null) {
            this.mPluginService.onStart(arg2, arg3);
        }
    }

    public int onStartCommand(Intent arg2, int arg3, int arg4) {
        int v0 = this.mPluginService != null ? this.mPluginService.onStartCommand(arg2, arg3, arg4) : super.onStartCommand(arg2, arg3, arg4);
        return v0;
    }

    @TargetApi(value = 14)
    public void onTaskRemoved(Intent arg2) {
        super.onTaskRemoved(arg2);
        if (this.mPluginService != null) {
            this.mPluginService.onTaskRemoved(arg2);
        }
    }

    public void onTrimMemory(int arg2) {
        super.onTrimMemory(arg2);
        if (this.mPluginService != null) {
            this.mPluginService.onTrimMemory(arg2);
        }
    }

    public boolean onUnbind(Intent arg2) {
        boolean v0 = this.mPluginService != null ? this.mPluginService.onUnbind(arg2) : super.onUnbind(arg2);
        return v0;
    }

    public void sendBroadcast(Intent arg2) {
        if (this.mPluginContextImpl != null) {
            this.mPluginContextImpl.sendBroadcast(arg2);
        } else {
            super.sendBroadcast(arg2);
        }
    }

    public void sendBroadcast(Intent arg2, String arg3) {
        if (this.mPluginContextImpl != null) {
            this.mPluginContextImpl.sendBroadcast(arg2, arg3);
        } else {
            super.sendBroadcast(arg2, arg3);
        }
    }

    public void sendOrderedBroadcast(Intent arg2, String arg3) {
        if (this.mPluginContextImpl != null) {
            this.mPluginContextImpl.sendOrderedBroadcast(arg2, arg3);
        } else {
            super.sendOrderedBroadcast(arg2, arg3);
        }
    }

    public void sendOrderedBroadcast(Intent arg9, String arg10, BroadcastReceiver arg11, Handler arg12, int arg13, String arg14, Bundle arg15) {
        if (this.mPluginContextImpl != null) {
            this.mPluginContextImpl.sendOrderedBroadcast(arg9, arg10, arg11, arg12, arg13, arg14, arg15);
        } else {
            super.sendOrderedBroadcast(arg9, arg10, arg11, arg12, arg13, arg14, arg15);
        }
    }

    @SuppressLint("MissingPermission")
    @TargetApi(value = 17)
    public void sendOrderedBroadcastAsUser(Intent arg10, UserHandle arg11, String arg12, BroadcastReceiver arg13, Handler arg14, int arg15, String arg16, Bundle arg17) {
        if (this.mPluginContextImpl != null) {
            this.mPluginContextImpl.sendOrderedBroadcastAsUser(arg10, arg11, arg12, arg13, arg14, arg15, arg16, arg17);
        } else {
            super.sendOrderedBroadcastAsUser(arg10, arg11, arg12, arg13, arg14, arg15, arg16, arg17);
        }
    }

    @SuppressLint("MissingPermission")
    @TargetApi(value = 17)
    public void sendStickyBroadcast(Intent arg2) {
        if (this.mPluginContextImpl != null) {
            this.mPluginContextImpl.sendStickyBroadcast(arg2);
        } else {
            super.sendStickyBroadcast(arg2);
        }
    }

    @SuppressLint("MissingPermission")
    @TargetApi(value = 17)
    public void sendStickyBroadcastAsUser(Intent arg2, UserHandle arg3) {
        if (this.mPluginContextImpl != null) {
            this.mPluginContextImpl.sendStickyBroadcastAsUser(arg2, arg3);
        } else {
            super.sendStickyBroadcastAsUser(arg2, arg3);
        }
    }

    @SuppressLint("MissingPermission")
    public void sendStickyOrderedBroadcast(Intent arg8, BroadcastReceiver arg9, Handler arg10, int arg11, String arg12, Bundle arg13) {
        if (this.mPluginContextImpl != null) {
            this.mPluginContextImpl.sendStickyOrderedBroadcast(arg8, arg9, arg10, arg11, arg12, arg13);
        } else {
            super.sendStickyOrderedBroadcast(arg8, arg9, arg10, arg11, arg12, arg13);
        }
    }

    @SuppressLint("MissingPermission")
    @TargetApi(value = 17)
    public void sendStickyOrderedBroadcastAsUser(Intent arg9, UserHandle arg10, BroadcastReceiver arg11, Handler arg12, int arg13, String arg14, Bundle arg15) {
        if (this.mPluginContextImpl != null) {
            this.mPluginContextImpl.sendStickyOrderedBroadcastAsUser(arg9, arg10, arg11, arg12, arg13, arg14, arg15);
        } else {
            super.sendStickyOrderedBroadcastAsUser(arg9, arg10, arg11, arg12, arg13, arg14, arg15);
        }
    }

    @TargetApi(value = 11)
    public void startActivities(Intent[] arg2) {
        if (this.mPluginContextImpl != null) {
            this.mPluginContextImpl.startActivities(arg2);
        } else {
            super.startActivities(arg2);
        }
    }

    @TargetApi(value = 16)
    public void startActivities(Intent[] arg2, Bundle arg3) {
        if (this.mPluginContextImpl != null) {
            this.mPluginContextImpl.startActivities(arg2, arg3);
        } else {
            super.startActivities(arg2, arg3);
        }
    }

    public void startActivity(Intent arg2) {
        if (this.mPluginContextImpl != null) {
            this.mPluginContextImpl.startActivity(arg2);
        } else {
            super.startActivity(arg2);
        }
    }

    @TargetApi(value = 16)
    public void startActivity(Intent arg2, Bundle arg3) {
        if (this.mPluginContextImpl != null) {
            this.mPluginContextImpl.startActivity(arg2, arg3);
        } else {
            super.startActivity(arg2, arg3);
        }
    }

    public ComponentName startService(Intent arg2) {
        ComponentName v0 = this.mPluginContextImpl != null ? this.mPluginContextImpl.startService(arg2) : super.startService(arg2);
        return v0;
    }

    public boolean stopService(Intent arg2) {
        boolean v0 = this.mPluginContextImpl != null ? this.mPluginContextImpl.stopService(arg2) : super.stopService(arg2);
        return v0;
    }

    private void tryLoadPlugin() {
        if (this.mPluginService == null) {
            PluginManager.getPluginManager();
            this.mPluginService = ComponentHelper.loadPluginService(PluginManager.getHostClassLoader(), this.getPluginClass());
            if (this.mPluginService != null) {
                this.mProxyRef = Reflect.on(this);
                this.mPluginRef = Reflect.on(this.mPluginService);
                ComponentHelper.attach(((Service) this), this.mPluginService, this.mProxyRef, this.mPluginRef);
            } else {
                this.stopSelf();
            }
        }
    }
}

